package com.pzl.library;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

/**
 * ImageView直接height=MarchParent；width=MarchParent;背景色不设置
 * 控制drawable的位置，缩放高度宽度
 */
public class ScaleImageView extends ImageView {
    String TAG = "ScaleImageView";
    private Context mContext;
    private Matrix mMatrix;
    private final float[] mMatrixValues = new float[9];

    private int mIntrinsicWidth_original;
    private int mIntrinsicHeight_original;

    /**
     * 不缩放，左边靠齐
     */
    public static final int TYPE_START = 0;
    /**
     * 不缩放，右边靠齐
     */
    public static final int TYPE_END = 1;
    /**
     * 不缩放，横向居中
     */
    public static final int TYPE_CENTER = 2;
    /**
     * 填充imageview宽度为基准，等比缩放
     */
    public static final int TYPE_FIX_WIDTH = 3;

    /**
     * 等比缩放填充imageview宽度，以右边为基点缩放
     */
    public static final int TYPE_FIX_WIDTH_AT_END = 4;
    /**
     * 等比缩放填充imageview宽度，以左边为基点缩放
     */
    public static final int TYPE_FIX_WIDTH_AT_START = 5;


    /**
     * 选择的初始化图片类型
     */
    private int TYPE = TYPE_START;

    private boolean isFirstDraw = false;

    public ScaleImageView(Context context) {
        this(context, null);
    }

    public ScaleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScaleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    private void initialize() {
        this.setScaleType(ScaleType.MATRIX);
        this.mMatrix = new Matrix();
        Drawable d = getDrawable();
        if (d != null) {
            mIntrinsicWidth_original = d.getIntrinsicWidth();
            mIntrinsicHeight_original = d.getIntrinsicHeight();
        }
    }

    /**
     * 设置bitmap图片
     *
     * @param bm
     * @param type
     */
    public void setBitmapImage(Bitmap bm, int type) {
        super.setImageBitmap(bm);
        this.TYPE = type;
        initialize();
    }

    /**
     * 设置资源图片
     *
     * @param resId
     * @param type
     */
    public void setResourceImage(int resId, int type) {
        super.setImageResource(resId);
        this.TYPE = type;
        initialize();
    }

    protected float getValue(Matrix matrix, int whichValue) {
        matrix.getValues(mMatrixValues);
        return mMatrixValues[whichValue];
    }

    /**
     * 获取图片缩放比
     *
     * @return
     */
    protected float getScale() {
        return getValue(mMatrix, Matrix.MSCALE_X);
    }

    /**
     * 获取图片X轴位移距离
     *
     * @return
     */
    public float getTranslateX() {
        return getValue(mMatrix, Matrix.MTRANS_X);
    }

    /**
     * 获取图片Y轴位移距离
     *
     * @return
     */
    protected float getTranslateY() {
        return getValue(mMatrix, Matrix.MTRANS_Y);
    }

    /**
     * 可见，可交互时候调用
     *
     * @param scale
     * @param x
     * @param y
     */
    public void zoomTo(float scale, int x, int y) {
        initType();
        //等比例缩放
        mMatrix.postScale(scale, scale);
        //type设定位置缩放
        switch (this.TYPE) {//TODO 现在是以drawablebitmap宽度小于getwidth的判断没做宽度大于的判断计算
            case TYPE_START:
                //Nothing to translate
                break;
            case TYPE_END:
                if (mIntrinsicWidth_original > getWidth()) {
                    mMatrix.postTranslate(getWidth() - getWidth() * scale, 0);
                } else {
                    mMatrix.postTranslate(getWidth() - mIntrinsicWidth_original * scale * 1.5f, 0);
                }
                break;
            case TYPE_CENTER:
                if (mIntrinsicWidth_original > getWidth()) {
                    mMatrix.postTranslate((mIntrinsicWidth_original / 2 - getWidth() / 2) * scale - (mIntrinsicWidth_original * scale / 2 - getWidth() / 2), 0);
                } else {
                    mMatrix.postTranslate(-mIntrinsicWidth_original * scale / 4 + getWidth() / 2 - mIntrinsicWidth_original * scale / 2, 0);
                }
                break;
            case TYPE_FIX_WIDTH:
                if (mIntrinsicWidth_original > getWidth()) {
                    float initScale = (float) getWidth() / (float) mIntrinsicWidth_original;//因为初始化位置缩放了一次
                    mMatrix.postTranslate(-((mIntrinsicWidth_original * initScale * scale) / 2) + getWidth() / 2, 0);
                } else {
                    mMatrix.postTranslate(-mIntrinsicWidth_original * scale / 4 + getWidth() / 2 - mIntrinsicWidth_original * scale / 2, 0);
                }
                break;
            case TYPE_FIX_WIDTH_AT_END:
                float initScale2 = (float) getWidth() / (float) mIntrinsicWidth_original;
                if (mIntrinsicWidth_original > getWidth()) {
                    mMatrix.postTranslate(getWidth() - mIntrinsicWidth_original * initScale2 * scale, 0);
                } else {
                    mMatrix.postTranslate(getWidth() - mIntrinsicWidth_original * initScale2 * scale, 0);
                }
                break;
            case TYPE_FIX_WIDTH_AT_START:
                //Nothing to do
                break;
        }
        // 移动到目标位置(每次相对于初始位置的)
        mMatrix.postTranslate(x, 0);
        mMatrix.postTranslate(0, y);
        setImageMatrix(mMatrix);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        if (isFirstDraw == false) {
            Log.i(TAG, "first initType");
            initType();
            isFirstDraw = true;
        }
        super.onDraw(canvas);
    }

    /**
     * 初始化图片显示形式
     */
    private void initType() {
        switch (TYPE) {
            case TYPE_START:
                initZoom(1, 0, 0);
                break;
            case TYPE_END:
                int dx = mIntrinsicWidth_original - getWidth();
                initZoom(1, -dx, 0);
                break;
            case TYPE_CENTER:
                int dx_center = (mIntrinsicWidth_original - getWidth()) / 2;
                initZoom(1, -dx_center, 0);
                break;
            case TYPE_FIX_WIDTH:
                float sclale;
                if (mIntrinsicWidth_original > getWidth()) {
                    sclale = (float) getWidth() / (float) mIntrinsicWidth_original;
                    initZoom(sclale, 0, 0);
                } else {
                    sclale = (float) getWidth() / (float) mIntrinsicWidth_original;
                    initZoom(sclale, 0, 0);
                }
                break;
            case TYPE_FIX_WIDTH_AT_END:
                float s_end = (float) getWidth() / (float) mIntrinsicWidth_original;
                initZoom(s_end, 0, 0);
                break;
            case TYPE_FIX_WIDTH_AT_START:
                float s_start = (float) getWidth() / (float) mIntrinsicWidth_original;
                initZoom(s_start, 0, 0);
                break;
        }
    }

    /**
     * 初始化显示
     *
     * @param scale
     * @param x
     * @param y
     */
    private void initZoom(float scale, int x, int y) {
        mMatrix.reset();
        //等比例缩放
        mMatrix.postScale(scale, scale);
        // 移动到目标位置
        mMatrix.postTranslate(x, 0);
        mMatrix.postTranslate(0, y);
        setImageMatrix(mMatrix);
    }

}
